Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
@react-hook/window-size
Advanced tools
React hooks for updating components when the size of the `window` changes.
@react-hook/window-size is a React hook that allows you to easily get the current window size and respond to window resize events. It provides a simple API to access the width and height of the window, making it useful for responsive design and dynamic layout adjustments.
Get Window Size
This feature allows you to get the current width and height of the window. The `useWindowSize` hook returns an array with the width and height, which you can use to render responsive components.
import { useWindowSize } from '@react-hook/window-size';
function MyComponent() {
const [width, height] = useWindowSize();
return (
<div>
<p>Window width: {width}px</p>
<p>Window height: {height}px</p>
</div>
);
}
Custom Throttle
This feature allows you to throttle the resize event updates. By passing a number to `useWindowSize`, you can control how often the window size is updated, which can improve performance by reducing the frequency of re-renders.
import { useWindowSize } from '@react-hook/window-size';
function MyComponent() {
const [width, height] = useWindowSize(250); // Throttle updates every 250ms
return (
<div>
<p>Window width: {width}px</p>
<p>Window height: {height}px</p>
</div>
);
}
react-use is a collection of essential React hooks, including a `useWindowSize` hook. It provides similar functionality to @react-hook/window-size but also includes a wide variety of other hooks for different use cases, making it a more comprehensive solution for managing state and side effects in React applications.
react-responsive is a package that allows you to create media query components in React. While it doesn't provide a direct hook for window size, it offers a more declarative approach to handling responsive design by allowing you to define breakpoints and render different components based on the current viewport size.
use-resize-observer is a React hook that uses the ResizeObserver API to track the size of a DOM element. While it is more focused on element size rather than window size, it can be used to achieve similar results by observing the document's root element. It provides more granular control over resizing behavior compared to @react-hook/window-size.
npm i @react-hook/window-size
React hooks for updating components when the size or orientation of the window
changes. These hooks come in two forms: debounced
using useDebounce()
and throttled using useThrottle()
.
Check out the example on CodeSandbox
//
// Debounced values
import {
useWindowSize,
useWindowWidth,
useWindowHeight,
} from '@react-hook/window-size'
const Component = (props) => {
const [width, height] = useWindowSize()
const onlyWidth = useWindowWidth()
const onlyHeight = useWindowHeight()
}
//
// Throttled values
import {
useWindowSize,
useWindowWidth,
useWindowHeight,
} from '@react-hook/window-size/throttled'
const Component = (props) => {
const [width, height] = useWindowSize()
const onlyWidth = useWindowWidth()
const onlyHeight = useWindowHeight()
}
A hook that returns the current width and height of the window. This hook is debounced, meaning it will wait (100ms by default) for the resize events to stop firing before it actually updates its state with the new width and height.
Type | Required? | Description | |
---|---|---|---|
options | DebouncedWindowSizeOptions | No | Options for configuring the hook. See DebouncedWindowSizeOptions below. |
Key | Type | Default | Description |
---|---|---|---|
wait | number | 100 | The amount of time in ms you want to wait after the latest resize event before updating the size of the window in state. |
leading | boolean | false | When true , updates the size of the window on the leading edge (right away) in addition to debouncing any additional events. |
initialWidth | number | 0 | The initial width to use when there is no window object, e.g. SSR |
initialHeight | number | 0 | The initial height to use when there is no window object, e.g. SSR |
[width: number, height: number]
Type | Description | |
---|---|---|
width | number | The current clientWidth of the window |
height | number | The current clientHeight of the window |
A hook that returns the current width of the window. This hook is debounced, meaning it will wait (100ms by default) for the resize events to stop firing before it actually updates its state with the new width.
Type | Required? | Description | |
---|---|---|---|
options | DebouncedWindowSizeOptions | No | Options for configuring the hook. See DebouncedWindowSizeOptions below. |
Key | Type | Default | Description |
---|---|---|---|
wait | number | 100 | The amount of time in ms you want to wait after the latest resize event before updating the size of the window in state. |
leading | boolean | false | When true , updates the size of the window on the leading edge (right away) in addition to debouncing any additional events. |
initialWidth | number | 0 | The initial width to use when there is no window object, e.g. SSR |
width: number
Type | Description | |
---|---|---|
width | number | The current clientWidth of the window |
A hook that returns the current height of the window. This hook is debounced, meaning it will wait (100ms by default) for the resize events to stop firing before it actually updates its state with the new height.
Type | Required? | Description | |
---|---|---|---|
options | DebouncedWindowSizeOptions | No | Options for configuring the hook. See DebouncedWindowSizeOptions below. |
Key | Type | Default | Description |
---|---|---|---|
wait | number | 100 | The amount of time in ms you want to wait after the latest resize event before updating the size of the window in state. |
leading | boolean | false | When true , updates the size of the window on the leading edge (right away) in addition to debouncing any additional events. |
initialHeight | number | 0 | The initial height to use when there is no window object, e.g. SSR |
height: number
Type | Description | |
---|---|---|
height | number | The current clientHeight of the window |
To use these throttled hooks instead of debounced hooks, import with import {...} from '@react-hook/window-size/throttled
A hook that returns the current width and height of the window. This hook is throttled, meaning it will only update its state at most 30fps (by default, configuration below) with the new width and height of the window. It will always update at the trailing edge, so you don't have to worry about not having the correct width or height after the window is finished resizing. It will also update at the leading edge if configured to do so.
Type | Required? | Description | |
---|---|---|---|
options | ThrottledWindowSizeOptions | No | Options for configuring the hook. See ThrottledWindowSizeOptions below. |
Key | Type | Default | Description |
---|---|---|---|
fps | number | 30 | The rate in frames per second at which the size of the window is updated |
leading | boolean | false | When true , updates the size of the window on the leading edge (right away) in addition to throttling any additional events. |
initialWidth | number | 0 | The initial width to use when there is no window object, e.g. SSR |
initialHeight | number | 0 | The initial height to use when there is no window object, e.g. SSR |
[width: number, height: number]
Type | Description | |
---|---|---|
width | number | The current clientWidth of the window |
height | number | The current clientHeight of the window |
A hook that returns the current width of the window. This hook is throttled, meaning it will only update its state at most 30fps (by default, configuration below) with the new width of the window. It will always update at the trailing edge, so you don't have to worry about not having the correct width after the window is finished resizing. It will also update at the leading edge if configured to do so.
Type | Required? | Description | |
---|---|---|---|
options | ThrottledWindowSizeOptions | No | Options for configuring the hook. See ThrottledWindowSizeOptions below. |
Key | Type | Default | Description |
---|---|---|---|
fps | number | 30 | The rate in frames per second at which the size of the window is updated |
leading | boolean | false | When true , updates the size of the window on the leading edge (right away) in addition to throttling any additional events. |
initialWidth | number | 0 | The initial width to use when there is no window object, e.g. SSR |
width: number
Type | Description | |
---|---|---|
width | number | The current clientWidth of the window |
A hook that returns the current height of the window. This hook is throttled, meaning it will only update its state at most 30fps (by default, configuration below) with the new height of the window. It will always update at the trailing edge, so you don't have to worry about not having the correct height after the window is finished resizing. It will also update at the leading edge if configured to do so.
Type | Required? | Description | |
---|---|---|---|
options | ThrottledWindowSizeOptions | No | Options for configuring the hook. See ThrottledWindowSizeOptions below. |
Key | Type | Default | Description |
---|---|---|---|
fps | number | 30 | The rate in frames per second at which the size of the window is updated |
leading | boolean | false | When true , updates the size of the window on the leading edge (right away) in addition to throttling any additional events. |
initialHeight | number | 0 | The initial height to use when there is no window object, e.g. SSR |
height: number
Type | Description | |
---|---|---|
height | number | The current clientHeight of the window |
MIT
FAQs
React hooks for updating components when the size of the `window` changes.
The npm package @react-hook/window-size receives a total of 112,793 weekly downloads. As such, @react-hook/window-size popularity was classified as popular.
We found that @react-hook/window-size demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.